<?php

/**
 * descargas actions.
 *
 * @package    ipdt.org
 * @subpackage descargas
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class descargasActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeDescargar(sfWebRequest $request)
  {


     $id_contenido=$request->getParameter('id');
     if($this->getUser()->isAuthenticated())
     {

             //recibe el nombre->cntenido
             $x=new Criteria();
             $x->add(DocumentosPeer::CONTENIDO,$id_contenido);
             $documento=DocumentosPeer::doSelectOne($x);

             if(is_object($documento))
             {
              $data = fread(fopen('uploads/documentos/'.$documento->getFilename(), "r"),
                      filesize('uploads/documentos/'.$documento->getFilename()));
              $response = $this->getContext()->getResponse();
              $response->setHttpHeader('Pragma:public', true);
              $response->setHttpHeader('Cache-Control', '');
              $response->setHttpHeader("Last-Modified", gmdate("D, d M Y H:i:s") . " GMT");
              $response->setHttpHeader('Content-Type', 'application/octet-stream');
              $response->setHttpHeader('Content-Disposition', 'attachment;filename="'.$documento->getFilename().'"');
              $response->setContent($data);

               return sfView::NONE;
             }
             else
             {
                 $this->redirect('descargas/fail');
             }
     }
     else
     {
            $this->getUser()->setFlash('sf_name', $id_contenido);
           $this->redirect('descargas/login');
     }



  }


   public function executeFail(sfWebRequest $request)
   {

   }

   public function executeLogin(sfWebRequest $request)
   {
        $variable = $this->getUser()->getFlash('sf_name', null);
        $this->idFile  = $variable;

   }

   public function executeAuth(sfWebRequest $request)
   {
          $usuario = $request->getParameter('usuario');
         $password = $request->getParameter('contrasena');
         $idDescarga = $request->getParameter('idFile');
         $criterio = new Criteria();
         $criterio->add(AsociadoPeer::LOGIN_ASOC, $usuario);
         $criterio->add(AsociadoPeer::CONTRASENA_ASOC, $password);
         $usuario = AsociadoPeer::doSelectOne($criterio);

         if(is_object($usuario))
         {
               $this->getUser()->setAuthenticated(true);
               $this->redirect('descargas/descargar?id='.$idDescarga);

         }
         else
         {
             $this->redirect('');
         }

   }



}
